<?
global $db;

skin_param("title","Map");

function _wiki_map() {
	global $db;
	
	$more = array();
	foreach ($db->query("select id,name,title,parent,link from wiki where latest = 1 order by name") as $e) $more[$e["parent"]][]=$e;
	
	$r = array();
	$top = "";
	if (!isset($more[$top])) return $r;
	
	$out=array_reverse($more[$top]);
	while (count($out)) {
        	$e=array_pop($out);
		$e["level"] = intval($e["level"]);
        	if (isset($more[$e["link"]])) {
			$a = array_reverse($more[$e["link"]]);
			foreach ($a as $k=>$ee) $a[$k]["level"] = $e["level"]+1;
			array_splice($out,count($out),0,$a);
			}
		$r[] = $e;
		}
	return $r;
	}

$r = _wiki_map($where);


echo "<div><p>";
foreach ($r as $e) {
	echo str_repeat("&nbsp",$e["level"]*4)."<a href=\"{$e["link"]}\">{$e["title"]}</a>";
	echo "<br>";
	}

echo "</p></div>";	
?>